//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/medicine_advice_model.dart';
import 'package:ls_work/net/response/medicine_advice/medicine_advice_list_data.dart';
import 'package:ls_work/util/ext_utils.dart';

class MedicineAdviceListViewModel extends BaseListViewModel<MedicineAdviceListData>{
  MedicineAdviceListViewModel(BuildContext context) : super(context);

  var model = MedicineAdviceModel();

  String? customerNameOrBedNoLike;

  var confirmList = [
    PickerDataWithIdModel(pickId: '2',pickName: '待确认'),
    PickerDataWithIdModel(pickId: '1',pickName: '已确认'),
  ];

  PickerDataWithIdModel? selectedConfirm;

   var checkList = [
     PickerDataWithIdModel(pickId: '2',pickName: '待核对'),
     PickerDataWithIdModel(pickId: '1',pickName: '已核对'),
   ];

  PickerDataWithIdModel? selectedCheck;


  var typeList = [
    PickerDataWithIdModel(pickName: '用药',pickId: '1'),
    PickerDataWithIdModel(pickName: '约束',pickId: '2'),
    PickerDataWithIdModel(pickName: '技术性护理',pickId: '3'),
    PickerDataWithIdModel(pickName: '辅具',pickId: '4'),
    PickerDataWithIdModel(pickName: '食谱',pickId: '5'),
    PickerDataWithIdModel(pickName: '注射',pickId: '6'),
    PickerDataWithIdModel(pickName: '营养补充剂',pickId: '7'),
    PickerDataWithIdModel(pickName: '其他',pickId: '8'),
  ];

  PickerDataWithIdModel? selectedType;

  String? createDate;

  @override
  Future<List<MedicineAdviceListData>> requestListData() async {
    return await model.medicineAdviceList(
      customerNameOrBedNoLike: customerNameOrBedNoLike,
      startDate: startDateStr,endDate: endDateStr,ordersConfirm:selectedConfirm?.pickId,createDate: createDate,ordersCheck: selectedCheck?.pickId,
      typeId:selectedType?.pickId,
      page: page,
      rows: pageLength
    );
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  void chooseMedicineType(){
    DataPickerView(context, typeList,currentFirstData: selectedType,onSubmitListener: (p1,p2,p3){
      selectedType = typeList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseConfirmStatus(){
    DataPickerView(context, confirmList,currentFirstData: selectedConfirm,onSubmitListener: (p1,p,p3){
      selectedConfirm = confirmList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseCheckStatus(){
    DataPickerView(context, checkList,currentFirstData: selectedCheck,onSubmitListener: (p1,p,p3){
      selectedCheck = checkList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseCreateDate(){
    DatePicker.showDatePicker(context,currentTime: createDate?.toDate(),onConfirm: (date){
      createDate = date!.toDateStr([yyyy,'-',mm,'-',dd]);
      notifyListeners();
      requestRefresh();
    });
  }

  void adviceConfirm(dynamic id){
    showApplyDialog(context: context, onSubmit: () async {
      showLoadingDialog(msg: '提交中...');

      try{
        await model.adviceConfirm(id);
        hideDialog();
        showToast('确认成功');
        requestRefresh();
      }catch(e){
        hideDialog();
        showToast(NetErrorUtils.getNetError(e));
      }
    },message: "确认要提交吗？");
  }

  void adviceCheck(dynamic id){
    showApplyDialog(context: context, onSubmit: () async {
      showLoadingDialog(msg: '提交中...');
      try{
        await model.adviceCheck(id);
        hideDialog();
        showToast('核对成功');
        requestRefresh();
      }catch(e){
        hideDialog();
        showToast(NetErrorUtils.getNetError(e));
      }
    },message: "确认要提交吗？");
  }

  void resetClick() {
    customerNameOrBedNoLike = null;
    selectedConfirm = null;
    selectedCheck = null;
    selectedType = null;
    createDate = null;
    startDateStr = "";
    startDateTime = null;
    endDateStr = "";
    endDateTime = null;
    notifyListeners();
    requestRefresh();
  }
}